<?php
session_start();
if ($_SESSION['auth'] != true && $_SESSION['rule'] != "READER") {
	die('Not log in');
}
include("includes/reader_lib.php");

switch($_GET['action']){
case "get_updated_item_time":
	echo getUpdateItemTime();
	break;
	
case "save_setting":
	setReaderSetting($_SESSION['userdata']['username'], $_GET['bgcolor'], $_GET['newsperpage'], $_GET['display']);
	echo "Saved setting";
	break;

case "changecolor":
	changeChannelColor($_SESSION['userdata']['username'],$_GET['id'], $_GET['color']);
	echo "Changed color to ".$_GET['color'];
	break;

case "unsubscribe":
	unSubscribe($_SESSION['userdata']['username'],$_GET['id']);
	echo "Unsubscribed";
	break;

case "subscribe":
	subscribe($_SESSION['userdata']['username'],$_GET['id']);
	echo "Subscribed";
	break;

case "update_item":
	$lastMo = lastModified();
	if($lastMo != $_SESSION['time_update_reader'] 
			|| empty($_SESSION['time_update_reader'])){
		$_SESSION['time_update_reader'] = $lastMo;
		echo "something has updated. blah...";
	}else{
		echo $NO_UPDATE;
	}
	break;
	
case "add_external_channel":
	print addExternalChannel($_SESSION['userdata']['username'], $_GET['title'] ,$_GET['link']);
	break;

case "remove_external_channel":
	removeExternalChannel($_SESSION['userdata']['username'], $_GET['id']);
	break;

case "show_update_item":
	$items = searchUpdateContent($_GET['username'], $_GET['keyword'], $_GET['channel_id']);
	echo json_encode($items);
	break;

case "set_read_item":
	$bool = set_item_read(intval($_GET['item_id']));
	echo $bool? "Already read" : "Fail";
	echo ": " . $_GET['item_id'];
	break;

case "message_number":
	
	$num_arr = getMsgNum($_GET['username'], $_GET['channel_id']);
	
	echo $num_arr['unread'] . ' unread message(s) from ' . $num_arr['all'] . ' messages.';
	break;

default:
	echo "Oh shit";
	break;
}
?>